// index.js
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '../../store/index'
import {
  assetsUrl
} from '../../network/requestUrl'
import {
  getHomeSwiperImgs,
  getCurrentMonthActive
} from '/../../network/index'
import {
  parsingCodeToken
} from '/../../network/api'
import {
  homeLastImg,
  hulims
} from '../../utils/imgConfig'
// 获取应用实例
const app = getApp()
const imgs1 = [
  `/images/pinpaijieshao.png`,
  `/images/yuezihuisuo.png`,
  `/images/miyuedaojia.png`,
  `/images/yuezican.png`,
  `/images/chankangfuwu.png`,
  `/images/xingjiyuesao.png`,
  `/images/xiangmuyuyue.png`,
  `/images/benyuehuodong.png`
]

const ts = [
  `/images/shubeiyuechengnuo.png`,
  `/images/10dayoushi.png`,
  `/images/baozheng.png`
]

Page({
  data: {
    navOne: ['品牌介绍', '月子会所', '蜜月到家', '月子餐外送', '产康服务', '星级月嫂', '项目预约', '本月活动'],
    imgs: imgs1,
    hulims: hulims,
    homeLastImg: homeLastImg,
    tsTitleIcon: ts,
    ts1Lable: [{
        name: '科学',
        info: '科学护理月子生活'
      },
      {
        name: '可靠',
        info: '无忧坐月子'
      },
      {
        name: '专业',
        info: '专人照顾'
      }
    ],
    ts2ys: [
      '24小时母婴师陪护',
      '私人订制套餐服务',
      '私人订制套餐服务',
      '医护人员上门检查',
      '管家式高端服务',
      '每日三餐三点送上门',
      '孕期增值服务',
      '个性化定制产后修复计划',
      '自营月嫂服务有保障',
      '月子会所团队一站式护理'
    ],
    ts3: [{
        name: '诚信保障',
        info1: '未服务',
        info2: '全额退款',
        icon: `/images/chengxin.png`
      },
      {
        name: '售后保障',
        info1: '售后团队24小时',
        info2: '在线解答',
        icon: `/images/shouhou.png`
      },
      {
        name: '人员保障',
        info1: '所有工作人员都',
        info2: '彻查背景信息',
        icon: `/images/renyuan.png`
      }
    ],
    swiperData: [], // 轮播图
    activeData: [], // 本月活动
    statusBarHeight: 0,
    navBarHeight: 0
  },

  onChangeStore() {
    wx.navigateTo({
      url: '/pages/storeSelection/index',
    })
  },
  getNavBarSize() {
    //获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight
    //获取小程序胶囊信息
    const menu = wx.getMenuButtonBoundingClientRect()
    //计算获得导航栏高度（此处为重点!!!）
    const navBarHeight = (menu.top - statusBarHeight) * 2 + menu.height
    this.setData({
      statusBarHeight: statusBarHeight,
      navBarHeight: navBarHeight
    })
  },
  async getCurrentData() {
    // siwper img
    const res = await getHomeSwiperImgs()
    const res1 = await getCurrentMonthActive()
    if (res.code === 200) {
      this.setData({
        swiperData: res.rows.map((item) => {
          item.images = `${assetsUrl}${item.images}`
          return item
        })
      })
    }
    if (res1.code === 200) {
      this.setData({
        activeData: res1.rows.map((item) => {
          item.eventsImage = `${assetsUrl}${item.eventsImage}`
          return item
        })
      })
    }
    console.log(res1)
  },
  // 点击跳转
  handleTo(e) {
    const {
      name
    } = e.currentTarget.dataset
    // 
    switch (name) {
      case '品牌介绍':
        wx.navigateTo({
          url: '/pages/brand/index',
        })
        break;
      case '月子会所':
        wx.navigateTo({
          url: '/pages/moonClub/index',
        })
        break;
      case '蜜月到家':
        wx.navigateTo({
          url: '/pages/honeyMoonHome/index',
        })
        break;
      case '月子餐外送':
        wx.navigateTo({
          url: '/pages/deliveryMeals/index',
        })
        break;
      case '产康服务':
        wx.navigateTo({
          url: '/pages/healthServices/index',
        })
        break;
      case '星级月嫂':
        wx.navigateTo({
          url: '/pages/starServices/index',
        })
        break;
      case '项目预约':
        wx.switchTab({
          url: '/pages/reservation/index',
        })
        break;
      case '本月活动':
        wx.navigateTo({
          url: '/pages/monthActivity/index',
        })
        break;
      default:
        break
    }
  },
  // token
  async getToken(code) {
    const res1 = await parsingCodeToken({
      code: code
    })
    if (res1.code === 200 && res1.data) {
      wx.setStorageSync('token', res1.data.token)
      wx.setStorageSync('sessionkey', res1.data.sessionkey)
      if (res1.data.username) {
        wx.setStorageSync('username', res1.data.username)
      }
      if (res1.data.avatar) {
        wx.setStorageSync('avatarUrl', res1.data.avatar)
      }
      this.getCurrentData()
    } else {
      // this.getCode()
    }
  },
  // 获取微信登陆code
  getCode() {
    const that = this
    wx.login({
      success: res => {
        if (res.code) {
          that.getToken(res.code)
        }
      }
    })
  },
  // 请求登陆
  // 请求手机号码
  // init
  onLoad() {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['curStore'],
      actions: ['updateStore']
    })
    this.getNavBarSize()
    const token = wx.getStorageSync('token')
    if (token && token !== '') {
      // 请求 获取当前页面数据
      this.getCurrentData()
    } else {
      // 登录
      this.getCode()
    }
  },
  // 护理模式更多
  onMoreOne() {
    wx.navigateTo({
      url: '/pages/nursingMode/index',
    })
  },
  // 本月活动更多
  onMoreTwo() {
    wx.navigateTo({
      url: '/pages/monthActivity/index',
    })
  },
})